package muse.widgets;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import muse.schedule.Task;

public class CalendarWidget extends Widget {
	private NamePanel namePanel;
	private CalendarPanel cPanel;
	private Calendar calendar;
	public CalendarWidget() {
		calendar = Calendar.getInstance();
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		String month = Month.values()[Calendar.getInstance().get(Calendar.MONTH)].name();
		Component c = Box.createRigidArea(new Dimension(320,30));
		add(c);
		add(cPanel = new CalendarPanel());
		add(namePanel = new NamePanel());
		namePanel.rename(month.toLowerCase().replaceFirst("" + month.toLowerCase().charAt(0), "" + month.charAt(0)));
		cPanel.refresh();
	}
	@Override
	public Dimension getPreferredSize() {
		return getSize();
	}
	public void refresh() {
		calendar = (Calendar)Calendar.getInstance().clone();
		cPanel.refresh();
	}

	@Override
	public String getIcon() {
		return "";
	}
	private enum Month {
		JANUARY,
		FEBRUARY,
		MARCH,
		APRIL,
		MAY,
		JUNE,
		JULY,
		AUGUST,
		SEPTEMBER,
		OCTOBER,
		NOVEMBER,
		DECEMBER;
	}
	private class CalendarPanel extends JPanel implements ActionListener {
		private JButton[][] buttons;
		public CalendarPanel() {
			setLayout(new GridLayout(7,7));
			String[] days = {" Sun "," Mon "," Tues "," Wed "," Thurs "," Fri "," Sat "};
			for(String s : days) {
				JPanel panel = new JPanel();
				panel.setPreferredSize(new Dimension(30,30));
				panel.add(new JLabel(s));
				panel.setBorder(BorderFactory.createLineBorder(Color.black));
				add(panel);
			}
			buttons = new JButton[6][7];
			for(int r = 0; r < buttons.length; r++)
				for(int c = 0; c < buttons[0].length; c++) {
					buttons[r][c] = new JButton();
					buttons[r][c].setPreferredSize(new Dimension(35,35));
					buttons[r][c].setFont(buttons[r][c].getFont().deriveFont(7.0f));
					add(buttons[r][c]);
				}
		}
		public void refresh() {
			while(calendar.get(Calendar.DATE) != 1)
				calendar.add(Calendar.DATE, -1);
			int month = calendar.get(Calendar.MONTH);
			for(int r = 0; r < buttons.length && calendar.get(Calendar.MONTH) == month; r++) {
				for(int c = 0; c < buttons[0].length && calendar.get(Calendar.MONTH) == month; c++) {
					buttons[r][c].setText("");
					buttons[r][c].setEnabled(false);
					buttons[r][c].setBackground(null);
				}
			}
			while(calendar.get(Calendar.MONTH) == month) {
				JButton cur = buttons[calendar.get(Calendar.WEEK_OF_MONTH) - 1][calendar.get(Calendar.DAY_OF_WEEK) - 1];
				cur.setEnabled(true);
				cur.setText("" + calendar.get(Calendar.DATE));
				calendar.add(Calendar.DATE, 1);
				for(int i = 0; i < Task.getIdCap(); i++) {
					Task t = Task.getTask(i);
					if(t == null)
						continue;
					if(!t.getSchedule().isScheduled(calendar))
						continue;
					// TODO: What if there are different types of tasks on a given day?
					switch(Integer.parseInt(t.getData("type"))) {
					case 0:	cur.setBackground(Color.yellow);	break;
					case 1: cur.setBackground(Color.cyan);		break;
					case 2: cur.setBackground(Color.green);		break;
					case 3: cur.setBackground(Color.pink);		break;
					}
				}
			}
			calendar.add(Calendar.DATE, -1);
			validate();
		}
		@Override
		public void actionPerformed(ActionEvent arg0) {
			
		}
	}
	private class NamePanel extends JPanel implements ActionListener {
		private JLabel name;
		public NamePanel() {
			setLayout(new BorderLayout());
			setBorder(BorderFactory.createLineBorder(Color.black, 2));
			name = new JLabel();
			add(name, BorderLayout.CENTER);
			JButton up = new JButton();
			up.setActionCommand("1");
			up.addActionListener(this);
			add(up, BorderLayout.EAST);
			JButton down = new JButton();
			down.setActionCommand("-1");
			down.addActionListener(this);
			add(down, BorderLayout.WEST);
		}
		public void rename(String month) {
			name.setText(month);
		}
		public String getName() {
			return name.getText();
		}
		public void actionPerformed(ActionEvent e) {
			int mod = Integer.parseInt(e.getActionCommand());
			int cur = Month.valueOf(getName().toUpperCase()).ordinal();
			int dest = mod + cur;
			if(dest < 0)
				dest = Month.values().length - 1;
			else if(dest >= Month.values().length)
				dest = 0;
			rename(Month.values()[dest].name().toLowerCase().replaceFirst(
					"" + Month.values()[dest].name().toLowerCase().charAt(0)
					, "" + Month.values()[dest].name().charAt(0)));
			calendar.add(Calendar.MONTH, mod);
			cPanel.refresh();
		}
	}
}
